#region

using System;
using System.Linq;
using System.Web.Mvc;
using log4net;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Controllers
{
    public class TechnologyController : BaseController<Technology>
    {
        private readonly TechnologyService _technologyService;

        public TechnologyController(TechnologyService technologyService)
        {
            _technologyService = technologyService;
        }


        public JsonResult List(string search = "", int pageIndex = 0, int pageSize = 10)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var page = search != ""
                               ? _technologyService.GetAllPagedSearched(search, pageIndex, pageSize)
                               : _technologyService.GetAllPaged(pageIndex, pageSize);
                var technologies = page.Entities.ToList().Select(t => new
                                                                          {
                                                                              t.Id,
                                                                              t.Created,
                                                                              t.Updated,
                                                                              t.Name
                                                                          });

                return
                    Json(
                        new
                            {
                                search,
                                page.Count,
                                page.CurrentPage,
                                page.PageSize,
                                page.PagesCount,
                                Entities = technologies
                            }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }


        [HttpPost]
        public JsonResult Save(Technology technology)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                _technologyService.SaveOrUpdate(technology);
                return Json(technology.Id, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult DeleteTechnology(int id)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var technology = _technologyService.GetById(id);
                _technologyService.Delete(technology);
                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetTechnologiesAutocomplete(string searchTerm, int maxRows)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var technologies = _technologyService.GetAll().OrderBy(t => t.Name).Select(t => new
                {
                    t.Name,
                    t.Id
                });
                if (searchTerm != "")
                {
                    technologies =
                        technologies.Where(l => l.Name.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
                }
                return Json(technologies, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}